<?php
namespace app\admin\controller;


use think\Controller;

use app\admin\service\User as UserService;
use think\Request;

class User extends Base
{


    /**
     * @auther zlq
     * @create_time 2020/5/21 16:35
     * @description 用户退出
     */
    public function logout()
    {
        session('user_id', false);
        session('user_name', false);
        return json(['status' => 1, 'msg' => '退出成功', 'data' => []]);
    }

    /**
     * @auther zlq
     * @create_time 2020/5/21 16:34
     * @description 用户修改密码页面
     * @return mixed
     */
    public function getPasswordEdit()
    {
        return $this->fetch('password');
    }

    /**
     * @auther zlq
     * @create_time 2020/5/21 16:34
     * @description 处理用户修改密码
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editPasswordHandle()
    {
        $password_old = input('oldPassword');
        $password_new = input('password');
        $password_new_re = input('repassword');
        $password_old = trim($password_old);
        $password_new = trim($password_new);
        $password_new_re = trim($password_new_re);
        if (!$password_old || !$password_new || !$password_new_re) {
            return json(['status' => -1, 'msg' => '参数不正确', 'data' => []]);
        }
        $UserService = new UserService();
        $res = $UserService->editPasswordHandle($password_old, $password_new, $password_new_re);
        switch ($res) {
            case 1:
                return json(['status' => 1, 'msg' => '密码修改成功', 'data' => []]);
                break;
            case -1:
                return json(['status' => -1, 'msg' => '旧密码输入不正确,请重试', 'data' => []]);
                break;
            case -2:
                return json(['status' => -2, 'msg' => '新密码输入不正确,请重试', 'data' => []]);
                break;
            case -3:
                return json(['status' => -3, 'msg' => '旧密码和新密码重复,请重试', 'data' => []]);
                break;
            case -4:
                return json(['status' => -4, 'msg' => '两遍输入的新密码不一致,请重试', 'data' => []]);
                break;
            case -10:
                return json(['status' => -5, 'msg' => '用户未登录登录,请重试', 'data' => []]);
                break;
            case -11:
                return json(['status' => -5, 'msg' => '用户信息不正确,请重试', 'data' => []]);
                break;
            case -12:
                return json(['status' => -5, 'msg' => '用户旧密码输入不正确,请重试', 'data' => []]);
                break;
            default:
                return json(['status' => -1, 'msg' => '登录失败请重试', 'data' => []]);
                break;
        }
    }

    /*获取用户列表*/
    public function getUserList(){
        $data = model('User','service') -> getUserList();
        $this -> assign('data',$data);

        return $this->fetch('/user/user_list');
    }

    /*加载添加用户页面*/
    public function getEdit(){

        $id = '';
        $id = input('id');
        $data['id']= '';
        $data['user_name']= '';
        $data['login_name'] = '';
        $data['login_password'] = '';
        $data['menu_auth'] = '';
        $data['user_status'] = '';
        $data['is_admin'] = '';
        $data['dd_user_id'] = '';
        $UserService = new UserService();
        if ($id) {
            $data = $UserService->getUserMsg($id);
        }
        //用户权限信息
        $auth = $UserService->getAuthMsg();
        // 钉钉用户列表
        $dd = $UserService->ddUserList();
        $this -> assign('data',$data);
        $this -> assign('auth',$auth);
        $this -> assign('dd',$dd);

        return $this->fetch('/user/user_edit');
    }

    /*保存编辑的用户信息*/
    public function saveEdit()
    {
        $data['user_name']= input('user_name');
        $data['dd_user_id'] = input('dd_user_id');
        $id = input("isEdit");
        if(!$id){
            $data['login_password'] = md5(input("login_password"));
            $data['register_date'] = date("Y-m-d");
            $data['register_time'] = date("H:i:s");
            $data['last_date'] = date("Y-m-d");
            $data['last_time'] = date("H:i:s");
        }
        $data['login_name'] = input("login_name");
        $data['menu_auth'] = input("menu_auth");
        $data['user_status'] = (int)input("user_status");
        $data['is_admin'] = (int)input("is_admin");
        $UserService = new UserService();
        $res = $UserService->saveEdit($data,$id);
        switch ($res) {
            case 1:
                return $this->apiSuccess("用户编辑成功");
                break;
            case 11:
                return $this->apiSuccess("用户新增成功");
                break;
            case -1:
                return $this->apiError("用户信息不正确");
                break;
            case -2:
                return $this->apiError("该用户名已被使用，无法重复使用");
                break;
            case -3:
                return $this->apiError("钉钉账户已被使用，无法重复使用");
                break;
            case -11:
                return $this->apiError("修改用户信息不正确");
                break;
            case -12:
                return $this->apiError("用户登录名已被使用，无法重复使用");
                break;
            default:
                return $this->apiError("用户保存失败,请重试");
                break;
        }
    }

    /*删除用户*/
    public function delUser()
    {
        $data['id'] = input('id');
        $UserService = new UserService();
        $res = $UserService->delUser($data);
        if ($res) {
            return $this->apiSuccess('用户已删除');
        } else {
            return $this->apiError('用户删除失败, 请重试');
        }
    }

    /*重置密码*/
    public function pwdReset()
    {
        $login_password = 'cxac2020.';
        $user_id = input('user_id');
        $UserService = new UserService();
        $res = $UserService->savePasswordEdit($login_password, $user_id);
        if ($res) {
            return $this->apiSuccess('密码重置已重置');
        } else {
            return $this->apiError('密码重置失败, 请重试');
        }
    }
}